Public Class frmNewUser
    Private db As Database = New Database

    Private Sub txtEmail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.TextChanged
        If validateEmail(txtEmail.Text) Then
            lblStatus.Text = ""
        Else
            lblStatus.Text = "Email address is invalid"
        End If
    End Sub

    Private Function validateEmail(ByVal email As String) As Boolean
        If Not email.Contains("@") Then Return False
        If Not email.Contains(".") Then Return False
        Return True
    End Function

    Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        If Not (txtPass1.Text = txtPass2.Text) Then
            lblStatus.Text = "Passwords do not match"
        ElseIf Not validateEmail(txtEmail.Text) Then
            lblStatus.Text = "Email address is invalid"
        Else
            If db.checkForEmail(txtEmail.Text.Trim) Then
                MsgBox("An account with that email address already exists.")
            ElseIf db.createAccount(txtEmail.Text, txtPass2.Text) Then

                'frmPrivateKey.BringToFront()
                MsgBox("User created successfully. This is your private key. Keep it safe.")
                frmPrivateKey.Show()
                frmLogin.Show()
                'frmPrivateKey.Hide()
                Me.Hide()
            Else
                MsgBox("User creation failed")
            End If
        End If
    End Sub

    Private Sub txtPass2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPass2.TextChanged
        If Not txtPass1.Text = txtPass2.Text Then
            lblStatus.Text = "Passwords do not match"
        Else
            lblStatus.Text = ""
        End If
    End Sub

    Private Sub frmNewUser_FormClosed(ByVal sender As System.Object, ByVal e As FormClosedEventArgs) Handles MyBase.FormClosed
        frmLogin.Show()
    End Sub
End Class